import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.List;
import java.util.Set;

public class demo01 {

    public static void test() throws InterruptedException {

        EdgeOptions options=Driver.getDriver();

        WebDriver driver = new EdgeDriver(options);

        driver.get("https://www.baidu.com/");
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
//        Thread.sleep(3000);

//        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).sendKeys("lebron james");
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("lebron james");

//        Thread.sleep(5000);

//        driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]")).click();
//        WebElement submitButton = wait.until(ExpectedConditions.elementToBeClickable
//                (By.xpath("//*[@id=\"chat-submit-button\"]")));
//        submitButton.click();
//        String text = driver.findElement(
//                        By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li/a/span[2]")).getText();
//        List<WebElement> webElementList = driver.findElements
//                (By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li/a/span[2]"));
//        System.out.println(webElementList.size());
//        for (WebElement element:webElementList){
//            System.out.println(element.getText());
//        }
//        System.out.println(text);
//        String origin=driver.getWindowHandle();
//        driver.findElement(By.xpath("//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a/span[2]")).click();
//
//        Set<String> windowHandles = driver.getWindowHandles();
//        String newWindow = null;
//        for (String window : windowHandles) {
//            if (!window.equals(origin)) {
//                newWindow = window;
//                break;
//            }
//        }
//        if (newWindow!=null){
//            driver.switchTo().window(newWindow);
//            driver.findElement(By.xpath("//*[@id=\"1\"]/div/div/div/div/div/div[2]/h3/a/span/em")).click();
//        }
//
//        String origin = driver.getWindowHandle();
//        driver.findElement
//                (By.xpath("//*[@id=\"i_cecream\"]/div[2]/div[1]/div[3]/div[2]/div[1]/a[8]")).click();
//        Set<String> windowHandles = driver.getWindowHandles();
//        String newHandle=null;
//        for (String window:windowHandles){
//            if(!origin.equals(window)){
//                newHandle=window;
//                break;
//            }
//        }
//        driver.switchTo().window(newHandle);
//        WebElement element = wait.until(ExpectedConditions.elementToBeClickable
//                (By.xpath("//*[@id=\"biliMainHeader\"]/div/div[1]/ul[2]/li[1]/li/div[1]/div/span")));
//        element.click();
//        driver.findElement(By
//                .xpath("//*[@id=\"biliMainHeader\"]/div/div[1]/ul[2]/li[1]/li/div[1]/div/span"))
//                .click();


//        driver.findElement(By.xpath("//*[@id=\"nav-searchform\"]/div[1]/input")).sendKeys("孤独摇滚");
//        WebElement element = wait.until(ExpectedConditions.elementToBeClickable
//                (By.xpath("//*[@id=\"nav-searchform\"]/div[2]")));


//        driver.findElement(By.cssSelector("#nav-searchform > div.nav-search-content > input")).sendKeys("孤独摇滚");
//        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#nav-searchform > div.nav-search-btn > svg")));
//        element.click();


//        driver.findElement(By.xpath("//*[@id=\"nav-searchform\"]/div[2]")).click();
//        driver.findElement(By.xpath("//*[@id=\"i_cecream\"]/div[2]/div[2]/div")).click();

//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("java113");
//        Thread.sleep(3000);
//
//        driver.findElement(By.cssSelector("#chat-textarea")).clear();
//
//        Thread.sleep(3000);
//
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("java114");
//        Thread.sleep(3000);

//        String text = driver.findElement(By.cssSelector("#chat-textarea")).getAttribute("placeholder");
//        System.out.println(text);
//        System.out.println(driver.getTitle());
//        System.out.println(driver.getCurrentUrl());

        System.out.println("页面切换前的title:"+driver.getTitle());
        System.out.println("页面切换前的url:"+driver.getCurrentUrl());
        Thread.sleep(3000);

        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();

        String origin=driver.getWindowHandle();
        Set<String> windowHandles = driver.getWindowHandles();
        for (String window:windowHandles){
            if (!origin.equals(window)){
                driver.switchTo().window(window);
            }
        }

        Thread.sleep(3000);

        System.out.println("页面切换前的title:"+driver.getTitle());
        System.out.println("页面切换前的url:"+driver.getCurrentUrl());

        driver.quit();
    }

    public static void main(String[] args) throws InterruptedException {
        test();
    }

}
